#[derive(Debug)]
struct MaxHeap { 
    data: Vec<i32> 
}  

impl MaxHeap {  
    fn new() -> Self {
        MaxHeap { data: Vec::new() }
    }

    // 上浮操作实现
    fn insert(&mut self, value: i32) { 
        /* TODO: 上浮调整 */ 
        self.data.push(value);
        let mut current = self.data.len() - 1;
        
        while current > 0 {
            let parent = (current - 1) / 2;
            if self.data[current] <= self.data[parent] {
                break;
            }
            self.data.swap(current, parent);
            current = parent;
        }
    }

    // 扩展功能（可选）
    #[allow(dead_code)]
    fn peek_max(&self) -> Option<i32> {
        self.data.first().copied()
    }  
}  

#[test] 
fn test_heap_insert() { 
    /* 验证堆性质 */ 
    let mut heap = MaxHeap::new();
    
    // 测试插入顺序不影响堆性质
    heap.insert(3);
    heap.insert(5);
    heap.insert(2);
    assert_eq!(heap.data[0], 5);
    //assert!(heap.data[1] >= heap.data[3]); // 左子节点检查
    //assert!(heap.data[2] >= heap.data[4]); // 右子节点检查
    
    // 测试批量插入
    for i in 1..=10 {
        heap.insert(i);
    }
    assert_eq!(heap.peek_max(), Some(10));
}  

// 添加堆操作演示
fn main() {
    let mut heap = MaxHeap::new();
    heap.insert(4);
    heap.insert(1);
    heap.insert(7);
    heap.insert(3);
    
    println!("Max heap structure:");
    println!("{:?}", heap.data);
    
    // 可视化堆结构
    println!("\nHeap as tree:");
    let mut level = 0;
    let mut count = 0;
    for (i, &val) in heap.data.iter().enumerate() {
        if i >= count {
            println!();
            count += 2usize.pow(level);
            level += 1;
        }
        print!("{} ", val);
    }
}